package nix.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nix.ssm.exception.MyException;
import nix.ssm.pojo.Item;
import nix.ssm.pojo.QueryVo;
import nix.ssm.service.ItemService;
import nix.utils.CommonUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ItemController {

	@Autowired
	private ItemService itemService;
	
//	@RequestMapping("/item/list.action")
//	public ModelAndView itemList() {
//		
//		List<Item> list = itemService.findList();
//		
//		ModelAndView mav = new ModelAndView();
//		mav.addObject("itemList", list);
//		mav.setViewName("itemList");
//		return mav;
//	}
	
	
//	@RequestMapping("/item/list.action")
//	public String itemList(Model model) throws MyException {
//		// int i = 1/0;
//		List<Item> list = itemService.findList();
//		if (true) {
//			throw new MyException("商品信息为空！");
//		}
//
//		model.addAttribute("itemList", list);
//		return "itemList";
//	}
	
	@RequestMapping("/item/list.action")
	public String itemList(Model model) {
		List<Item> list = itemService.findList();
		
		model.addAttribute("itemList", list);
		return "itemList";
	}
	
//	@RequestMapping("/item/list.action")
//	public void itemList(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		
//		List<Item> list = itemService.findList();
//
//		model.addAttribute("itemList", list);
//		request.getRequestDispatcher("/WEB-INF/jsp/itemList.jsp").forward(request, response);
//	}
	
	@RequestMapping("/item/edit.action")
	// public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response) {
	// public ModelAndView toEdit(@RequestParam(value="id",required=true,defaultValue="1") Integer id) {
	public ModelAndView toEdit(Integer id) {
		
		// String id = request.getParameter("id");
		// Item item = itemService.findById(Integer.parseInt(id));
		
		Item item = itemService.findById(id);
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("item", item);
		mav.setViewName("editItem");
		return mav;
	}
	
	@RequestMapping("/item/save.action")
	// public ModelAndView save(QueryVo vo) {
	// public ModelAndView save(Item item) {
	public String save(Item item, MultipartFile pictureFile) throws Exception {
		
		// 保存图片
		String picName = CommonUtils.uuid();
		String oriName = pictureFile.getOriginalFilename();
		String extName = oriName.substring(oriName.lastIndexOf("."));
		pictureFile.transferTo(new File("d:/upload/" + picName + extName));
		item.setPic(picName + extName);
		
		// itemService.update(vo.getItem());		
		itemService.update(item);
		
//		ModelAndView mav = new ModelAndView();
//		mav.setViewName("success");
//		return 
		return "redirect:/item/list.action";
	}
	
	@RequestMapping(value="/item/deletes.action")
	// public ModelAndView deletes(Integer[] ids) {
	public ModelAndView deletes(QueryVo vo) {	
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("success");
		return mav;
	}
	
	@RequestMapping("/item/itemjson")
	public @ResponseBody Item jsontest(@RequestBody Item item) {
		System.out.println(item);
		return item;
	}
	
	@RequestMapping("item/{id}")
	@ResponseBody
	public Item findById(@PathVariable Integer id) {
		Item item = itemService.findById(id);
		return item;
	}
}
